//
// Copyright (c) 2024 BPer
//

#include <bperice/BPerRtcSocket.h>
#include <bperrtc/BPerPush.h>
#include <bperutil/sys/BPerLog.h>

int32_t bPer_send_avpacket(BPerRtcSession *session, BPerRtpPacket *pkt,	BPerBuffer *pbuf) {

	int32_t err = BPer_Ok;
	int32_t nn_encrypt = bPer_buffer_pos(pbuf);

	if (session->push)	session->push->cache_nack(session->push->pubStream, pkt, pbuf->data,nn_encrypt);
#if BPer_Enable_Dtls
	if ((err = bPer_enc_rtp(&session->context.srtp, pbuf->data, &nn_encrypt)) != BPer_Ok) {
		return bPer_error_wrap(err, "srtp protect");
	}
#endif

	return session->context.sock->write(&session->context.sock->session, pbuf->data, nn_encrypt);
}

int32_t bPer_send_nackpacket(BPerRtcContext *context, char *data, int32_t nb) {
	int32_t err = BPer_Ok;
	int32_t nn_encrypt = nb;
#if BPer_Enable_Dtls
	if ((err = bPer_enc_rtp(&context->srtp, data, &nn_encrypt)) != BPer_Ok) {
		return bPer_error_wrap(err, "srtp protect");
	}
#endif
	return context->sock->write(&context->sock->session, data, nn_encrypt);
}
